import sys
sys.path.append('./')
import os
import xlwt
import math
import numpy as np

run = '1572168562'
path = '/home/wanglinhui/警戒空域/收拾旧山河/runs/{}'.format(run)
vals = []
tests = []
with open(os.path.join(path, 'log.txt'), 'r') as reader:
	for line in reader.readlines():
		line = line.strip()
		if 'val_time' in line:
			vals.append(list(map(lambda x: float(x.split(':')[-1]),line.split(',')[1:])))
		if 'test_time' in line:
			tests.append(list(map(lambda x: float(x.split(':')[-1]),line.split(',')[1:])))
# print(vals)
# with open(os.path.join(path, '{}_log_analysic.txt'.format(run)), 'w') as writer:
# 	for i in range(len(vals)):
# 		str1 = '\t'.join(list(map(lambda x: x.split(':')[-1], vals[i])))
# 		str2 = '\t'.join(list(map(lambda x: x.split(':')[-1], tests[i])))
# 		writer.write('{}\t{}\n'.format(str1, str2))

workbook = xlwt.Workbook()
sheet = workbook.add_sheet('sheet1')

for i in range(len(vals)):
	for j in range(3):
		sheet.write(i, j, float(vals[i][j]))
		sheet.write(i, j+3, float(tests[i][j]))
vals = np.array(vals)
tests = np.array(tests[:-1])        # 删掉最后一个，因为最后一个是倒数第二个的重复，删掉之后，len(vals)==len(tests)
sheet.write(i+1, 0, round(sum(vals[29:, 0])/21, 4))
sheet.write(i+1, 1, round(sum(vals[29:, 1])/21, 4))
sheet.write(i+1, 3, round(sum(tests[29:, 0])/21,4))
sheet.write(i+1, 4, round(sum(tests[29:, 1])/21, 4))
# print(vals[29:31,0])
# print(round(sum(tests[29:, 0])/21,4))
# print(round(sum(tests[29:, 1])/21,4))
# print(tests[29:,0])

workbook.save(os.path.join(path, '{}_log_analysic.xls'.format(run)))
